package org.ch03.job.test05;

import java.lang.reflect.Field;

/**
 * @author XCH
 * @date 2025 - 03 - 28
 */

public class Student {
    private String name;
    @AgeRestriction(min = 18, message = "年龄不符要求")
    private int age;

    public Student() {
    }

    public Student(String name, int age) throws Exception {
        this.name = name;
        setAge(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws Exception {
        checkAge(age);
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    public void checkAge(int age) throws Exception {
        Field fields = this.getClass().getDeclaredField("age");
        AgeRestriction annotation = fields.getAnnotation(AgeRestriction.class);
        if (annotation != null) {
            if (age < annotation.min()) {
                throw new IllegalArgumentException(annotation.message());
            }else {
                System.out.println("年龄符合要求");
            }
        }
    }
}
